﻿using IBll;
using IDal;
using Model;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;

namespace Bll
{
    public class MemberBll : IMemberBll
    {
        public MemberBll(IBaseDal<Member>memberDal,IBaseDal<MemberLevle>memberlevelDal)
        {
            MemberDal = memberDal;
            MemberlevelDal = memberlevelDal;
        }

        public IBaseDal<Member> MemberDal { get; }
        public IBaseDal<MemberLevle> MemberlevelDal { get; }

        public async Task<int> AddMember(Member member)
        {
            string s = DateTime.Now.ToString("yyyyMMddHHmmss");
            string n = new Random().Next(100,1000).ToString();
     //       member.GetHashCode=
            return await MemberDal.AddAsync(member);
        }

        public List<MemberLevle> GetMemberLevle()
        {
            return MemberlevelDal.GetAllAsync().ToList();
        }

        public dynamic GetMember()
        {
            var huiyuan = MemberDal.GetAllAsync();
            var huiyuanlevel = MemberlevelDal.GetAllAsync();
            var m = from a in huiyuan
                    join b in huiyuanlevel
                    on a.LevelId equals b.LevelId
                    select new
                    {
                        a.Id,
                        a.Name,
                        a.Pwd,
                        a.State,
                        a.LevelId,
                        b.LevelName,
                        a.IsDel,
                        a.CreateUserId,
                        a.CreateDate,
                        a.DelUserId,
                        a.DelDate,
                        a.UpdaUserId,
                        a.UpdaDate
                        };
            return m.ToList();
        }

        public async Task<int> IsDelMember(int id)
        {
            var m = MemberDal.GetAllAsync().FirstOrDefault(x => x.Id == id);
            m.IsDel = false;
            m.DelDate = DateTime.Now;
            return await MemberDal.UpdaAsync(m);
        }

        public Member GetUpdaMember(int id)
        {
            return MemberDal.GetAllAsync().FirstOrDefault(x => x.Id == id);
        }

        public async Task<int> UpdaMember(Member member)
        {
            member.UpdaDate = DateTime.Now;
            return await MemberDal.UpdaAsync(member);
        }
    }
}
